package br.com.mmm.javacenter.framework.view.taglib.helper;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;

import br.com.mmm.javacenter.framework.reflection.ReflectionFacade;
import br.com.mmm.javacenter.framework.view.ContextAttributes;


public class Input extends Element{
	
	protected String name;
	protected String id;
	protected Object value;
	protected String label;
	protected boolean somenteLeitura;
	protected boolean inicioLinha;
	protected boolean finalLinha;
	protected int maxLength;
	protected ReflectionFacade rf;
	
	public Input() {
		
	}
	public Input(JspContext context, JspFragment fragment) {
		super(context, fragment);
	}
	
	
	public void make() throws JspException,IOException  {
		if(inicioLinha){
			finalLinha = false;
			setRequestAttribute(ContextAttributes.REQUEST_MESMA_LINHA,true);
		}else if(finalLinha){
			inicioLinha = false;
			setRequestAttribute(ContextAttributes.REQUEST_MESMA_LINHA,false);
		}else{
			if (getRequestAttribute(ContextAttributes.REQUEST_MESMA_LINHA)!=null&&(Boolean) getRequestAttribute(ContextAttributes.REQUEST_MESMA_LINHA))
				inicioLinha = finalLinha = false;
			else
				inicioLinha = finalLinha = true;
		}
		
		before();
		if(inicioLinha) {
			writer.write("<tr>");
		}
		
		writeLabel();
		
		writer.write("<td>");
		if(beforeInput()){
			writer.write("<input ");
			addAttributesDefault(writer);
			if(somenteLeitura){
				writer.write(getAtributo("readonly", "readonly"));
			}
			attributes();
			writer.write(" >");
			writeBody(writer);
			writer.write("</input>");
			afterInput();
		}
		writer.write("</td>");
		afterCellInput();
		if(finalLinha) {
			writer.write("</tr>");
		}
		after();
	}
	public void addAttributesDefault(Writer writer) throws IOException {
		writer.write(getAtributo("name",name));
		writer.write(getAtributo("id",id));
		writer.write(getAtributo("value",value));
		writer.write(getAtributo("maxLength",maxLength));
		writer.write(getAtributo("size",maxLength));
	}
	public void before() throws IOException,JspException{}
	public void attributes()throws IOException{}
	public void after()throws IOException{}
	public void afterInput()throws IOException,JspException{}
	public void afterCellInput()throws IOException{}
	public boolean beforeInput()throws IOException{return true;}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}


	public Object getValue() {
		return value;
	}


	public void setValue(Object value) {
		this.value = value;
		rf = new ReflectionFacade(value);
	}


	public String getLabel() {
		return label;
	}


	public void setLabel(String label) {
		this.label = label;
	}


	public boolean isSomenteLeitura() {
		return somenteLeitura;
	}


	public void setSomenteLeitura(boolean somenteLeitura) {
		this.somenteLeitura = somenteLeitura;
	}


	public boolean isInicioLinha() {
		return inicioLinha;
	}


	public void setInicioLinha(boolean inicioLinha) {
		this.inicioLinha = inicioLinha;
	}


	public boolean isFinalLinha() {
		return finalLinha;
	}


	public void setFinalLinha(boolean finalLinha) {
		this.finalLinha = finalLinha;
	}


	public int getMaxLength() {
		return maxLength;
	}


	public void setMaxLength(int maxLength) {
		this.maxLength = maxLength;
	}
	protected void writeLabel(String label) throws IOException {
		if(label!=null){
			writer.write("<td class='labelInput'>");
			writer.write(label + (label.endsWith(":")?"&nbsp;&nbsp;":":&nbsp;&nbsp;"));
			writer.write("</td>");
		}
	}
	protected void writeLabel() throws IOException {
		if(label!=null){
			writeLabel(label);
		}
	}
}
